package com.woniuxy.educationplatform.controller;


import com.woniuxy.educationplatform.bean.*;
import com.woniuxy.educationplatform.mapper.MsgTaskMapper;
import com.woniuxy.educationplatform.mapper.SmsManageMapper;
import com.woniuxy.educationplatform.service.ISMSService;
import com.woniuxy.educationplatform.service.ISmsManage;
import com.woniuxy.educationplatform.service.IStudentService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/sms")
@RestController
public class SMSController {

    @Resource
    private ISMSService smsServiceImpl;
    @Resource
    private ISmsManage smsManageImpl;
    @Resource
    private IStudentService studentServiceImpl;


    //获取短信签名
    @RequestMapping(value="/sign",produces = {"application/json;charset=utf-8"})
    public List<SmsSignBean> selectAllSMSSign() {
        return smsServiceImpl.selectAllSMSSign();
    }


    //获取短信模板
    @RequestMapping(value="/archetype",produces = {"application/json;charset=utf-8"})
    public List<SmsArchetypeBean> selectAllSmsArchetype(){
        return smsServiceImpl.selectAllSmsArchetype();
    }


    //获取消息类型
    @RequestMapping(value="/type",produces = {"application/json;charset=utf-8"})
    public List<MsgTypeBean> selectAllMsgType(){
        return smsServiceImpl.selectAllMsgType();
    }


    //群发短信  向数据库中插入数据
    @PostMapping(value="/insert",produces = {"application/json;charset=utf-8"})
    public void  insert(boolean send_to_all, Integer selected_schoolID, String TaskName, String TaskDesc,
                        Integer fkMsgTypeId, Integer fkSmsSignId, Integer fkSmsArchetypeId, String sendingTime){

        List<SmsManageBean> smsList = new ArrayList<>();
        List<StudentBean> studentList = null;
        Integer schoolID = 0; //0代表发送给所有人
        if(send_to_all){
            studentList = studentServiceImpl.findAllByParams();
        }else{
            studentList = studentServiceImpl.findStudentBySchoolID(selected_schoolID);
            schoolID = selected_schoolID;
        }



        //创建任务，获取返回的任务ID
        MsgTaskBean msgTaskBean = new MsgTaskBean();
        msgTaskBean.setNameValue(TaskName);
        msgTaskBean.setDescValue(TaskDesc);
        java.sql.Timestamp time = new Timestamp(new java.util.Date().getTime());
        msgTaskBean.setGmtCreate(time);
        msgTaskBean.setSchoolID(schoolID);
        msgTaskBean.setMsgTypeID(fkMsgTypeId);
        smsManageImpl.Insert(msgTaskBean);



        //遍历studentList，创建List<SmsManageBean>
        for (StudentBean student: studentList )
            {
                //根据List<StudentBean>里的ID创建List<SmsManageBean>
                SmsManageBean bean = new SmsManageBean();
                bean.setFkUserId(student.getId());
                bean.setSendingTime(sendingTime);
                bean.setGmtCreate(new Timestamp(new java.util.Date().getTime()));
                bean.setFkMsgTypeId(fkMsgTypeId);
                bean.setFkMsgTaskId(msgTaskBean.getId());
                bean.setFkSmsSignId(fkSmsSignId);
                bean.setFkSmsArchetypeId(fkSmsArchetypeId);

                smsList.add(bean);
        }

        //smsList.forEach(e-> System.out.println(e));

        smsManageImpl.InsertListOfSmsManageBean(smsList);

    }






    //测试查询所有任务
    @RequestMapping(value = "/alltask", produces = {"application/json;charset=utf-8"})
    public List<MsgTaskBean> selectAllTask(String SMS_type_selectedID, String keyWord, String sendingTime, String ID){
        MsgTaskBean msgTaskBean = new MsgTaskBean();

//        System.out.println(SMS_type_selectedID == null);
//        System.out.println("".equals(SMS_type_selectedID));
//        System.out.println("null".equals(SMS_type_selectedID));

        if((SMS_type_selectedID != null) && (!"".equals(SMS_type_selectedID) && (!"null".equals(SMS_type_selectedID)))){
            msgTaskBean.setMsgTypeID(Integer.valueOf(SMS_type_selectedID));
        }

        if((keyWord != null) && (!"".equals(keyWord)) && (!"null".equals(keyWord))){
            msgTaskBean.setNameValue(keyWord);
        }

        if((sendingTime != null) && (!"".equals(sendingTime)) && (!"null".equals(sendingTime))){
            java.sql.Timestamp time = java.sql.Timestamp.valueOf(sendingTime);
            msgTaskBean.setGmtCreate(time);
        }

        if((ID != null) && (!"".equals(ID)) && (!"null".equals(ID))){
            msgTaskBean.setId(Long.valueOf(ID));
        }



        List<MsgTaskBean> list = smsManageImpl.selectAll(msgTaskBean);




        //list.forEach(e-> System.out.println(e));
        System.out.println(SMS_type_selectedID);
        System.out.println(keyWord);
        System.out.println(sendingTime);
        System.out.println(msgTaskBean);
        System.out.println("--------------------------------------");
        return list;
    }





    //逻辑删除(即修改）
    @PostMapping(value="/delete",produces = {"application/json;charset=utf-8"})
    public void deleteByID(Long[] selectedID){

        for (Long id:selectedID) {
            System.out.println(id);
            smsManageImpl.deleteByID(id);//循环遍历，逻辑删除
        }

    }


    //根据任务ID查询短信发送记录
    @RequestMapping(value="/selectRecordByTaskID",produces = {"application/json;charset=utf-8"})
    List<SmsManageBean> selectRecordByTaskID(Long[] taskID){

        List<SmsManageBean> list = new ArrayList<>();


        for (Long id : taskID){
            System.out.println("-----------");
            System.out.println(id);
            list.addAll(smsManageImpl.selectRecordByTaskID(id));
        }

        return list;
    }

    @Resource
    SmsManageMapper smsManageMapperImpl;

    @RequestMapping(value="/test",produces = {"application/json;charset=utf-8"})
    List<StuVideosBean> selectTest(String name){

        String str = "棋魂";
        return  smsManageMapperImpl.selectTest(str);
    }



    //更新任务表数据
    @PostMapping(value="/updateTask",produces = {"application/json;charset=utf-8"})
    public void updateTask(MsgTaskBean msgTaskBean){
        smsManageImpl.updateTask(msgTaskBean);
    }




}
